<?php
/*
 * Create by Thai Hien
 * User: TH
 * Date: 2015/02/15
 * Time: 24:00
 * */
class THclass{
	public static function phpThumb($path, $width = '', $height = '', $crop = FALSE)
	{
		//if ($path == '')
		return $path;
		if (strstr($path, 'http'))
			return $path;
		$path = urlencode($_SERVER['HTTP_HOST'] . $path);
		// $return = 'http://cdn.wapme.vn/thumbnail/phpThumb.php?src=http://' . $path . '&w=' . $width . '&h=' . $height;
		$return = '/phpthumb/phpThumb.php?src=' . $path . '&w=' . $width . '&h=' . $height;
		if ($crop)
			$return .= '&zc=1';
		return $return;
	}
	public static function listSidemenu(){
		$node = '<ul class="has-mainmenu">';
		$categories = ProductsGroup::model()->findAll(array('condition' => 'del_flg = 0'));
		foreach ($categories as $cat){
			$typecats = ProductsType::model()->findAll(array('condition' => 'del_flg = 0'));
			$group_name = $cat->group_name;
			$node .= '<li class="has-sub">
						<div class="cat-name">
							<i class="fa fa-cogs"></i><span> '.$group_name.'</span></div>';
			if(isset($typecats)){
				$node .='<ul>';
				foreach ($typecats as $tcat){
					if($cat->id == $tcat->group_id){
						$type_name = $tcat->type_name;
						$node .='<li>'.CHtml::link('<i class="fa fa-angle-double-right"></i><span>'.$type_name.'</span>',  
								Yii::app()->urlManager->createUrl('/product/list', array('id' => $tcat->id))).'</li>';	
					}
				}
				$node .='</ul>';
			}
			$node .='</li>';
		}
		$node .= '</ul>';
		echo $node;
	}
	// Bo dau tieng viet
	public static function convert_name_vi($str) {
		$unicode = array(
           'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
           'd'=>'đ',
           'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
           'i'=>'í|ì|ỉ|ĩ|ị',
           'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
           'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
           'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
           'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
           'D'=>'Đ',
           'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
           'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
           'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
           'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
           'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
       );

      foreach($unicode as $nonUnicode=>$uni){
           $str = preg_replace("/($uni)/i", $nonUnicode, $str);
      }
      $str = str_replace(' ', '-', $str);
      $str = str_replace(',', '', $str);
      $str = str_replace('+', '', $str);
      $str = str_replace('--', '-', $str);
      return mb_strtolower($str);
	
	}
	
	public static function rand_string($length){
		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str='';
		$size = strlen( $chars );
		for( $i = 0; $i < $length; $i++ ) {
			$str .= $chars[ rand( 0, $size - 1 ) ];
		}
		return $str;
	}
	public static function getSort($name) {
		$sort	= '';
		if (isset($_POST['name_sort'])) {
			$sortName	= $_POST['name_sort'];
			if ($sortName == $name) {
				$sort	= $_POST['type_sort'];
			}
		}
		return $sort;
	}
}